package com.example.gameapplication.view;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import com.example.gameapplication.database.DataUtils;
import java.util.concurrent.ExecutorService;
//游戏背景移动图
public class MainGameBgView extends SurfaceView implements SurfaceHolder.Callback, Runnable , DataUtils.PlayButtonCallBack {
    private SurfaceHolder mHolder;
    private ExecutorService executorService;
    private Canvas mCanvas;
    private Paint paint;
    private Bitmap background2;
    private Bitmap background;
    private float scalex;
    private float scaley;
    private float bg_y; // 图片的坐标
    private float bg_y2;
    private boolean startScreen;//后台回来画面是黑色的，需要播放一帧。
    public MainGameBgView(Context context) {
        super(context);
        init();
    }
    public MainGameBgView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }
    public MainGameBgView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }
    protected void init() {
        mHolder = getHolder();
        mHolder.addCallback(this);
        setFocusable(true);
        setFocusableInTouchMode(true);
        this.setKeepScreenOn(true);
        executorService = DataUtils.getExecutorService();//用线程池，不要频繁创建线程，否则app会崩。这里我使用单线程的线程池。
    }
    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        Resources resources = getResources();

        background = DataUtils.getBackground(resources);
        background2 = DataUtils.getBackground2(resources);
        bg_y = DataUtils.getBg_y();
        bg_y2 = DataUtils.getBg_y2();
        DataUtils.setGameViewWidth(getMeasuredWidth());
        DataUtils.setGameViewHight(getMeasuredHeight());
        scalex = (float) (1.0 * getMeasuredWidth() / background.getWidth());//图片宽高比例与view的宽高比例
        scaley = (float) (1.0 * getMeasuredHeight() / background.getHeight());
        paint = DataUtils.getPaint();
        startScreen = true;
        DataUtils.setBgPlayButtonCallBack(this);
        executorService.execute(this);
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        DataUtils.setIsPlaying(false);
        if (!background.isRecycled()) {
            background.recycle();
        }
        if (!background2.isRecycled()) {
            background2.recycle();
        }
    }


    @Override
    public void run() {
        while (DataUtils.isIsPlaying()||startScreen) {
            startScreen = false;//播放完1帧之后改为false
            try {

                long startTime = System.currentTimeMillis();
                //锁定画布并返回画布对象
                mCanvas = mHolder.lockCanvas();
//                mCanvas = mHolder.lockHardwareCanvas();//硬件加速的画板，但是当cpu高使用率，比如说战机同时出现好几百台，反而会卡，表现不平稳
                //接下去就是在画布上进行一下draw
                mCanvas.drawColor(Color.WHITE);
                mCanvas.save();
                // 计算背景图片与屏幕的比例
                mCanvas.scale(scalex, scaley, 0, 0);//控制第一第二参数使得手机屏幕满屏，其实可以计算的	canvas.scale(scalex, scaley, 0, 0);
//                if (background==null||background2==null||background.isRecycled()||background2.isRecycled()){
//                    background = DataUtils.getBackground(getResources());
//                    background2 = DataUtils.getBackground2(getResources());
//                }
                mCanvas.drawBitmap(background, 0, bg_y, paint); // 绘制背景图
                mCanvas.drawBitmap(background2, 0, bg_y2, paint); // 绘制背景图
                mCanvas.restore();

//                DataUtils.runSkillAttack();//因为性能问题，不把他放到run里边运算，毕竟大招不可能一直在使用状态所以在 skill.drawSelf(mCanvas);中调用
//                DataUtils.runWindmillArrack();// 因为性能问题，不把他放到run里边运算，毕竟大招不可能一直在使用状态所以在windmillSkill.drawSelf(mCanvas);中调用
                DataUtils.runEnemyBulletAttack();//敌方子弹攻击
                DataUtils.runAttack();//我方子弹与敌方战机相遇，和我方战机与地方战机碰撞
                DataUtils.runGetBuff();//检查是否获取升级物品
                DataUtils.checkCustoms();//是否关卡模式，根据当前得分提高难度
                viewLogic(); // 背景移动的逻辑
                long endTime = System.currentTimeMillis();
                if (startTime - endTime < 80) {
                    Thread.sleep(60);
                }
            } catch (Exception e) {
                Log.e("AACC", "Exception:MainGameView: " + e.toString());
                e.printStackTrace();
            } finally {
                //当画布内容不为空时，才post，避免出现黑屏的情况。
                if (mCanvas != null)
                    mHolder.unlockCanvasAndPost(mCanvas);
            }
        }
    }

    // 背景移动的逻辑函数
    public void viewLogic() {
        if (bg_y > bg_y2) {
            bg_y += 10;
            bg_y2 = bg_y - background.getHeight();
        } else {
            bg_y2 += 10;
            bg_y = bg_y2 - background.getHeight();
        }
        if (bg_y >= background.getHeight()) {
            bg_y = bg_y2 - background.getHeight();
        } else if (bg_y2 >= background.getHeight()) {
            bg_y2 = bg_y - background.getHeight();
        }
        DataUtils.setBg_y(bg_y);
        DataUtils.setBg_y2(bg_y2);
    }

    @Override
    public void playButtonOncleck() {
        startScreen = true;
        executorService.execute(this);
    }
}